﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using Bymed.SpineScan.DataModel;
using Bymed.SpineScan.Service.Validation;

namespace Bymed.SpineScan.WPF.ValidationRules
{
    public class PatientNumberValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();

            var patient = new Patient
            {
                Number = value.ToString().Trim()
            };

            if (!patientlValidator.CheckRequiredNumber(patient))
            {
                return new ValidationResult(false, patientlValidator.GetError(PatientValidator.NumberErrorKey));
            }
            else if (!patientlValidator.CheckNumberLength(patient))
            {
                return new ValidationResult(false, patientlValidator.GetError(PatientValidator.NumberErrorKey));
            }

            return new ValidationResult(true, null);
        }
    }

    public class PatientNameValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();

            var patient = new Patient
            {
                Name = value.ToString().Trim()
            };

            if (!patientlValidator.CheckRequiredName(patient))
            {
                return new ValidationResult(false, patientlValidator.GetError(PatientValidator.NameErrorKey));
            }
            else if (!patientlValidator.CheckNameLength(patient))
            {
                return new ValidationResult(false, patientlValidator.GetError(PatientValidator.NameErrorKey));
            }

            return new ValidationResult(true, null);
        }
    }

    public class PatientHeightValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var height = value as string;
            short result;

            if (!string.IsNullOrEmpty(height) && !short.TryParse(height, out result))
            {
                return new ValidationResult(false, "身高只能为数字");
            }

            return new ValidationResult(true, null);
        }
    }

    public class PatientWeightValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var weight = value as string;
            short result;

            if (!string.IsNullOrEmpty(weight) && !short.TryParse(weight, out result))
            {
                return new ValidationResult(false, "体重只能为数字");
            }

            return new ValidationResult(true, null);
        }
    }

    public class PatientRemarkValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();

            var patient = new Patient
            {
                Remark = value.ToString().Trim()
            };

            if (!patientlValidator.CheckRemarkLength(patient))
            {
                return new ValidationResult(false, patientlValidator.GetError(PatientValidator.RemarkErrorKey));
            }

            return new ValidationResult(true, null);
        }
    }
}
